
#include "mainmenue.h"

typedef void func_t(void);
typedef struct mainmenue{
    char key;
    char list_num;
    char list_totle_num;
}mainmenue_t;
mainmenue_t my_menue;
void mainmenue_init(){
    my_menue.key = 0;
    my_menue.list_num = 0;
    my_menue.list_totle_num = 2;
}
func_t* fun;

func_t* funs[2] ={
    book_test,
    test_audio,
};
void main_exit(){
    fun = menue_task;
}
void show_mainmenue(void){
    static char state = 0;
    switch(state){
        case 0:
            mainmenue_init();
            state = 1;
        break;
        case 1:
            OLED_Clear(); 
            draw_string(16,16*0 ,16,"电子书");
            draw_string(16,16*1 ,16,"音乐播放器");
            draw_char_EN(0,16*my_menue.list_num ,16,'>');
            state = 2;
        break;
        case 2:
            if(my_menue.key == 1){
                my_menue.key = 0;
                my_menue.list_num++;
                my_menue.list_num = my_menue.list_num % my_menue.list_totle_num;
                for(int i = 0; i < my_menue.list_totle_num;i++){
                    draw_char_EN(0,16*i ,16,' ');
                }
                draw_char_EN(0,16*my_menue.list_num ,16,'>');
            }
            if(my_menue.key == 2){
                state = 0;
                fun = funs[my_menue.list_num];
            }
            
        break;
    }
}
void menue_task(void){
    char key_num;
    while(1){
        
        key_num = key_scan();
        if(key_num){
            my_menue.key = key_num;
        }
        show_mainmenue();
        if(my_menue.key == 2){
            my_menue.key = 0;
            break;    
        }
        delay_ms(10);
    }
}
void main_menue(){
    fun = menue_task;
    //fun = book_test;
    while(1){
        fun();
    }
}
void draw_line_list(uint16_t num){
    char data[64][7];
    char i,j;
    int line;
    if(num > 100){
        num = 100;
    }
    for(i = 0; i < 64; i++){
        for(j = 0; j < 7; j++){
            if( (j == 2) || (j==3)){
                data[i][j] = 0xff;
            }else{
                data[i][j] = 0x0;
            }
        }
    }
    line = 64 * num /100;
    if(line >= 59){
        line = 59;
    }
    for(i = 0;i<4;i++){
        for(j = 0; j < 6; j++){
            data[i+line][j] = 0xff;
        }
    }
    OLED_DrawBitmap(120, 0, 7, 64, (const uint8_t *)data);
}

